package customize

import (
	"gitcode.com/G-YT/baoyou/server/global"
	"gitcode.com/G-YT/baoyou/server/model/common/request"
	"gitcode.com/G-YT/baoyou/server/model/common/response"
	"gitcode.com/G-YT/baoyou/server/model/customize"
	customizeReq "gitcode.com/G-YT/baoyou/server/model/customize/request"
	customizeRes "gitcode.com/G-YT/baoyou/server/model/customize/response"
	"gitcode.com/G-YT/baoyou/server/service"
	"gitcode.com/G-YT/baoyou/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"strconv"
	"strings"
)

type EfficacyApi struct {
}

var efficacyService = service.ServiceGroupApp.CustomizeServiceGroup.EfficacyService

// GetEfficacyList 获取功效列表
// @Tags Assess
// @Summary 获取功效列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.User true "获取功效列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/assess/getEfficacyList [get]
func (e *EfficacyApi) GetEfficacyList(c *gin.Context) {
	var ae customizeRes.EfficacyList
	uid := utils.GetUserID(c)
	userEfficacy, _ := UserService.FindUserByWhere(map[string]interface{}{"id": uid}, "efficacy_tag_ids")
	efficacyList, _ := EfficacyService.GetEfficacyList()
	if userEfficacy.EfficacyTagIDs != "" {
		ae.IsExist = true
	} else {
		ae.IsExist = false
	}
	// 填充ae结构体
	for _, v := range efficacyList {
		list := customizeRes.List{
			Name:    v.Name,
			IsExist: false,
			ID:      strconv.Itoa(int(v.ID)),
		}
		// 检查功效ID是否在e_ids字段中
		if userEfficacy.EfficacyTagIDs != "" {
			ids := strings.Split(userEfficacy.EfficacyTagIDs, ",")
			for _, id := range ids {
				if id == strconv.Itoa(int(v.ID)) {
					list.IsExist = true
					break
				}
			}
		}
		ae.List = append(ae.List, list)
	}
	response.Result(200, ae, "获取成功", c)
}

// SaveUserEfficacyTag 保存用户功效标签
// @Tags Assess
// @Summary 提交用户功效标签
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query user.User true "保存用户功效标签"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"保存成功"}"
// @Router /api/efficacy/saveUserEfficacyTag [post]
func (e *EfficacyApi) SaveUserEfficacyTag(c *gin.Context) {
	var req struct {
		EIDs string `json:"e_ids" form:"e_ids"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	ID := utils.GetUserID(c)
	if err := UserService.UpdateDataById(ID, map[string]interface{}{"efficacy_tag_ids": req.EIDs}); err != nil {
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// CreateEfficacy 创建功效
// @Tags Efficacy
// @Summary 创建功效
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customize.Efficacy true "创建功效"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /efficacy/createEfficacy [post]
func (e *EfficacyApi) CreateEfficacy(c *gin.Context) {
	var efficacy customize.Efficacy
	err := c.ShouldBindJSON(&efficacy)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := efficacyService.CreateEfficacy(&efficacy); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteEfficacy 删除功效
// @Tags Efficacy
// @Summary 删除功效
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customize.Efficacy true "删除功效"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /efficacy/deleteEfficacy [delete]
func (e *EfficacyApi) DeleteEfficacy(c *gin.Context) {
	var efficacy customize.Efficacy
	err := c.ShouldBindJSON(&efficacy)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := efficacyService.DeleteEfficacy(efficacy); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteEfficacyByIds 批量删除功效
// @Tags FdEfficacy
// @Summary 批量删除功效
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除功效"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /efficacy/deleteEfficacyByIds [delete]
func (e *EfficacyApi) DeleteEfficacyByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := efficacyService.DeleteEfficacyByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateEfficacy 更新功效
// @Tags Efficacy
// @Summary 更新功效
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body customize.Efficacy true "更新功效"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /efficacy/updateEfficacy [put]
func (e *EfficacyApi) UpdateEfficacy(c *gin.Context) {
	var efficacy customize.Efficacy
	err := c.ShouldBindJSON(&efficacy)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := efficacyService.UpdateEfficacy(efficacy); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindEfficacy 用id查询功效
// @Tags FdEfficacy
// @Summary 用id查询功效
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customize.Efficacy true "用id查询功效"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /efficacy/findEfficacy [get]
func (e *EfficacyApi) FindEfficacy(c *gin.Context) {
	var efficacy customize.Efficacy
	err := c.ShouldBindQuery(&efficacy)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if efficacyList, err := efficacyService.GetEfficacy(efficacy.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(efficacyList, c)
	}
}

// GetEfficacyListByWhere 分页获取功效列表
// @Tags FdEfficacy
// @Summary 分页获取功效列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query healthReq.FdEfficacySearch true "分页获取功效列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /efficacy/getEfficacyListByWhere [get]
func (e *EfficacyApi) GetEfficacyListByWhere(c *gin.Context) {
	var pageInfo customizeReq.EfficacySearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := efficacyService.GetEfficacyInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

// SetEfficacyArticle 设置功效文章关系
// @Tags Efficacy
// @Summary 设置功效文章关系
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customizeReq.EfficacyArticleReq true "设置功效文章关系"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
// @Router /efficacy/setEfficacyArticle [post]
func (e *EfficacyApi) SetEfficacyArticle(c *gin.Context) {
	var req customizeReq.EfficacyArticleReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := efficacyService.SetEfficacyArticle(req); err != nil {
		global.GVA_LOG.Error("设置失败!", zap.Error(err))
		response.FailWithMessage("设置失败", c)
	} else {
		response.OkWithMessage("设置成功", c)
	}
}

// GetEfficacyArticle 获取功效文章关系
// @Tags Efficacy
// @Summary 获取功效文章关系
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query customizeReq.EfficacyArticleReq true "获取功效文章关系"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
// @Router /efficacy/getEfficacyArticle [post]
func (e *EfficacyApi) GetEfficacyArticle(c *gin.Context) {
	efficacyID := c.Query("efficacyID")
	if res, err := efficacyService.GetEfficacyArticle(efficacyID); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithData(res, c)
	}
}
